<template>
  <div>
    <div :class="showTotal ? 'total-introduce' : 'detailed-introduce'">
      <div class="intro-content" :title="showText" ref="desc">
        <span class="merchant-desc" v-if="showText">
          {{ showText }}
        </span>
        <div class="unfold" @click="showTotalIntro" v-if="showExchangeButton">
          <p>{{ exchangeButton ? "展开" : "收起" }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    text: {
      type: String,
      default: function() {
        return "";
      }
    }
  },
  data() {
    return {
      showText: "",
      // 是否展示所有文本内容
      showTotal: true,
      // 显示展开还是收起
      exchangeButton: true,
      // 是否显示展开收起按钮
      showExchangeButton: false
    };
  },
  watch: {
    showText: function(newValue, oldValue) {


      this.$nextTick(() => {

        // 判断介绍是否超过四行
        if (!this.$refs.desc) {

          return;
        }
        let descHeight = window.getComputedStyle(this.$refs.desc).height;



        if (parseInt(descHeight) > 42) {

          // 显示展开收起按钮
          this.showExchangeButton = true;
          this.exchangeButton = true;
          // 不是显示所有
          this.showTotal = false;
        } else {
          // 不显示展开收起按钮
          this.showExchangeButton = false;
          // 没有超过二行就显示所有
          this.showTotal = true;
        }
      });
    }
  },
  computed: {},
  methods: {
    showTotalIntro() {
      this.showTotal = !this.showTotal;
      this.exchangeButton = !this.exchangeButton;
    }
  },
  created() {},
  mounted() {
    this.showText = this.text;
  }
};
</script>
<style lang="scss" scoped>
.total-introduce {
  height: auto;
  overflow: hidden;
  font-size: 12px;
  color: #434343;
  .intro-content {
    .merchant-desc {
      width: 100%;
      line-height: 21px;
    }
  }
  .unfold {
    display: block;
    z-index: 11;
    float: right;
    width: 40px;
    height: 21px;
    text-align: center;
  }
}
.detailed-introduce {
  font-size: 12px;
  color: #434343;
  position: relative;
  overflow: hidden;
  .intro-content {
    // 最大高度设为2倍的行间距
    max-height: 42px;
    line-height: 21px;
    word-wrap: break-word;
    /*强制打散字符*/
    word-break: break-all;
    background: #ffffff;
    /*同背景色*/
    color: #ffffff;
    overflow: hidden;
    .merchant-desc {
      width: 100%;
      line-height: 21px;
    }
    &:after,
      // 这是展开前实际显示的内容
      &:before {
      content: attr(title);
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      color: #434343;
      // overflow: hidden;
    }
    // 把最后最后一行自身的上面三行遮住
    &:before {
      display: block;
      overflow: hidden;
      z-index: 1;
      max-height: 21px;
      background: #ffffff;
    }
    &:after {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      height: 42px;
      /*截取行数*/
      -webkit-line-clamp: 2;
      text-overflow: ellipsis;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      /*行首缩进字符数，值为[(截取行数-1)*尾部留空字符数]*/
      text-indent: -12em;
      /*尾部留空字符数*/
      padding-right: 4em;
    }
    .unfold {
      z-index: 11;
      width: 40px;
      height: 21px;
      outline: 0;
      position: absolute;
      right: 0;
      bottom: 0;
      text-align: center;
    }
  }
}

.unfold {
  p {
    color: #7fbe87;
    font-size: 14px;
    font-weight: 500;
    margin: 0;
    line-height: 21px;
  }
}
</style>
